use "created_dta\analysis_samp_w_outcomes", clear
drop if female == .

preserve 
use "raw_dta\cas_unit_deploy_nopii_v3.dta", clear


keep pid dodgrp incident_city incident_country incident_state incidentdt_s dnu2 compt suicide death
sort pid incidentdt_s
by pid: gen id_n = _n
gen incident_myr = ym(year(incidentdt_s),month(incidentdt_s))
reshape wide dodgrp incident_city incident_country incident_state incidentdt_s incident_myr dnu2 compt suicide death, i(pid) j(id_n)
tempfile cas
save `cas'
restore 
merge 1:1 pid using `cas', nogen keep(1 3)

gen cas_death_myr = incident_myr1 if death1 == 1
gen cas_death_country = incident_country1 if death1 == 1
forvalues i = 1/5 {
	replace cas_death_myr = incident_myr`i' if death`i' == 1
	replace cas_death_country = incident_country`i' if death1 == `i'
}


qui {
egen mos_post_yr_terms = group(init_pmos post_orgraa init_terms year_arrival)
egen mos_tc_post_yr_terms = group(init_pmos_cons post_orgraa init_terms year_arrival)
egen mos_tc_post_qtr_terms = group(init_pmos_cons post_orgraa init_terms qtr)
egen dmsl_qtr = group(dmsl_tc_first qtr)
egen dmsl_month = group(dmsl_tc_first month_arrival)
egen dmsl_yr = group(dmsl_tc_first year_arrival)
g all = 1 
g terms3 = init_terms == "3"

g bal_samp = (year_arrival<=2011)


g anydepc = init_nrdepc > 0 & init_nrdepc < . 


forvalues y = 1(1)36 {
 g noncom_death_by_`y' = ndi_death_by_`y' - combat_death_by_`y'
 replace noncom_death_by_`y' = 0 if noncom_death_by_`y' == -1
 g ndi_nondespnoncom_by_`y' = ndi_death_by_`y' - ndi_war_by_`y' - ndi_despair2_by_`y'
g despairv3_by_`y' = (ndi_drug2_by_`y' == 1 | ndi_suicide_plus2_by_`y' ==1  )
 g intentdrug_by_`y' = (ndi_drug_self2_by_`y' == 1 | ndi_drug_und2_by_`y' == 1) 
  g ndi_suic_by_`y' = ndi_suicide_plus2_by_`y'
    g wia_kia_by_`y' = max(wia_by_`y', kia_by_`y')
  
  g inc_combined_by_`y' = (inc_by_`y' == 1 | incarcerated_by_`y' == 1) if inc_by_`y' != .
  
  g anyssdissi_in_`y' = anyssdi_in_`y' == 1 | anyssi_in_`y' == 1
  
  foreach dv in profile sigprofile phys sigphys psychiatric sigpsychiatric othprof sigothprof {
  	replace any_`dv'_by_`y' = 0 if missing(any_`dv'_by_`y')
  }
  g any_phys_disab_by_`y' = any_phys_by_`y' == 1 & disabsep_by_`y' == 1
  g any_psych_disab_by_`y' = any_psychiatric_by_`y' == 1 & disabsep_by_`y' == 1
  g any_phys_sep_by_`y' = any_phys_by_`y' == 1 & sep_by_`y' == 1
  g any_psych_sep_by_`y' = any_psychiatric_by_`y' == 1 & sep_by_`y' == 1
  

  
  g ndi_mv_acc2_AFIZ_by_`y' = ndi_mv_acc2_by_`y' == 1 & inlist(cas_death_country,"AF","IZ","KU")
  g ndi_mv_acc2_US_by_`y' = ndi_mv_acc2_by_`y' == 1 & !inlist(cas_death_country,"AF","IZ","KU")
  
  }
  
  replace any_premier_v1_2_all2870689 = 0 if premier_v1_2_all2870689 == 99
  replace any_premier_v1_2_all2875689 = 0 if premier_v1_2_all2875689 == 99
  replace any_p13_all2870731 = 0 if p13_all2870731 == 99
  replace any_p13_all2875731 = 0 if p13_all2875731 == 99

  replace premier_v1_2_all2870689 = 0 if premier_v1_2_all2870689 == 99
  replace premier_v1_2_all2875689 = 0 if premier_v1_2_all2875689 == 99
  replace p13_all2870731 = 0 if p13_all2870731 == 99
  replace p13_all2875731 = 0 if p13_all2875731 == 99
 
g mths_dep_win_3yr = deployed_0 + deployed_1 + deployed_2 + deployed_3 ///
+ deployed_4 + deployed_5 + deployed_6 + deployed_7 ///
+ deployed_8 + deployed_9 + deployed_10 + deployed_11 ///
+ deployed_12 + deployed_13 + deployed_14 + deployed_15 ///
+ deployed_16 + deployed_17 + deployed_18 + deployed_19 ///
+ deployed_20 + deployed_21 + deployed_22 + deployed_23 ///
+ deployed_24 + deployed_25 + deployed_26 + deployed_27 ///
+ deployed_28 + deployed_29 + deployed_30 + deployed_31 ///
+ deployed_32 + deployed_33 + deployed_34 + deployed_35

bys dmsl_tc_first qtr : egen meandep = mean(mths_dep_win_3yr)  
bys dmsl_tc_first qtr: egen ct = total(1) 
g instrument_mths_dep_qtr = ((meandep*ct) - mths_dep_win_3yr)/(ct-1)

gen kia_by_12_adj = kia_by_12 
replace kia_by_12_adj = 0 if dep_win_3yr == 0  
bys dmsl_tc_first qtr: egen meankia = mean(kia_by_12_adj) 
g kiarate = ((meankia*ct) - kia_by_12_adj)/(ct - 1)
		assert kiarate>-.000001
		replace kiarate = 0 if kiarate<0 & kiarate>-.000001
		
gen wia_by_12_adj = wia_by_12 
replace wia_by_12_adj = 0 if dep_win_3yr == 0 
bys dmsl_tc_first qtr: egen meanwia = mean(wia_by_12_adj) 
gen wiarate = ((meanwia*ct) - wia_by_12_adj)/(ct-1)
		assert wiarate>-.000001 
		replace wiarate = 0 if wiarate<0 & wiarate>-.000001

	gen combat_death_by_12_adj = combat_death_by_12 
replace combat_death_by_12_adj = 0 if dep_win_3yr == 0  
bys dmsl_tc_first qtr: egen meancombat = mean(combat_death_by_12_adj) 
g comrate = ((meancombat*ct) - combat_death_by_12_adj)/(ct - 1)
		assert comrate>-.000001
		replace comrate = 0 if comrate<0 & comrate>-.000001
	
	gen cas_by_12_adj = (combat_death_by_12_adj==1|wia_by_12_adj==1)
	bys dmsl_tc_first qtr: egen meancas = mean(cas_by_12_adj) 
	gen casrate = ((meancas*ct) - cas_by_12_adj)/(ct-1)

foreach n in cas kia wia com {
	gen `n'rate_notsd=`n'rate
	sum `n'rate
	replace `n'rate = (`n'rate - r(mean))/r(sd)
}

drop meandep ct 

foreach n in kia wia cas com {
	gen ${NEWINST}_`n'rate = ${NEWINST} * `n'rate
	gen ${NEWENDOG}_`n'rate = ${NEWENDOG} * `n'rate
}

	gen ${NEWINST}_casnotsd = ${NEWINST} * `n'casrate_notsd *100
	gen ${NEWENDOG}_`n'casnotsd = ${NEWENDOG} * `n'casrate_notsd * 100
	gen casnotsd = casrate_notsd * 100
	
bys dmsl_tc_first month_arrival : egen meandep = mean(mths_dep_win_3yr)
bys dmsl_tc_first month_arrival: egen ct = total(1)
g instrument_mths_dep_mth = ((meandep*ct) - mths_dep_win_3yr)/(ct-1)
drop meandep ct 

bys dmsl_tc_first year_arrival : egen meandep = mean(mths_dep_win_3yr)
bys dmsl_tc_first year_arrival: egen ct = total(1)
g instrument_mths_dep_yr = ((meandep*ct) - mths_dep_win_3yr)/(ct-1)
drop meandep ct 

bys dmsl_tc_first qtr : egen meandep = mean(dep_win_3yr)
bys dmsl_tc_first qtr: egen ct = total(1)
g instrument_any_dep_qtr = ((meandep*ct) - dep_win_3yr)/(ct-1)
drop meandep ct 

g mths_dep_by_q1 = deployed_0 + deployed_1 + deployed_2
forvalues x = 2(1)36 {
	local i = 3*(`x'-1)
	local j = `i' + 1
	local k = `j' + 1
	local z = `x' - 1
	g mths_dep_by_q`x' = deployed_`i' + deployed_`j' + deployed_`k' + mths_dep_by_q`z'
} 

gen init_cmf = substr(init_pmos_cons,1,2)
foreach x in 13 19 68 91 92 {
g init_mos`x' = init_cmf == "`x'"
}

egen mos_bct_yr_terms = group(init_pmos_cons bct_type year_arrival init_terms)
egen mos_bct_qtr_terms = group(init_pmos_cons bct_type qtr init_terms)
g init_afqsc_2 = init_afqsc^2
g init_afqsc_3 = init_afqsc^3

foreach ls in cl co el fa gm gt mm of sc { 
	g `ls'_mi = ace_first_asvab`ls' == .
	replace ace_first_asvab`ls' = 0 if ace_first_asvab`ls' == .
}

local finalcleanXs init_approx_age init_white init_black init_hispanic afqs_mi  any_moral_waiver init_afqsc init_afqsc_2 init_afqsc_3 married anydepc hsd_ged high_school  any_meps_med_fail_cd any_meps_alcohol_fail  any_meps_marijuana_fail any_meps_cocaine_fail any_meps_alcohol_tst_miss any_meps_marijuana_tst_miss any_meps_cocaine_tst_miss ace_first_asvabcl ace_first_asvabco ace_first_asvabel ace_first_asvabfa ace_first_asvabgm ace_first_asvabgt ace_first_asvabmm ace_first_asvabof ace_first_asvabsc cl_mi co_mi el_mi fa_mi gm_mi gt_mi mm_mi of_mi sc_mi

foreach xvar in `finalcleanXs' {
	gen `xvar'7mdp = `xvar'*${NEWENDOG}
}
foreach xvar in `finalcleanXs' {
	gen `xvar'7ins = `xvar'*${NEWINST}
}
gen ${NEWENDOG}_2=${NEWENDOG}^2
gen ${NEWINST}_2=${NEWINST}^2

gen casrate_permo=casrate_notsd/${NEWINST}
sum casrate_permo, d
gen cascat1=(casrate_permo<`r(p50)')
gen cascat2=(casrate_permo>=`r(p50)' & casrate_permo!=.)

forvalues k=1(1)2{
	gen ${NEWENDOG}_cascat`k'=${NEWENDOG}*cascat`k'
	gen ${NEWINST}_cascat`k'=${NEWINST}*cascat`k'
}

gen anycas=(casrate_notsd>0)
	gen ${NEWENDOG}_anycas=${NEWENDOG}*anycas
	gen ${NEWINST}_anycas=${NEWINST}*anycas

foreach var of varlist combat_death_by_* noncom_death_by_* ndi_war_by_* ndi_despair2_by_* ndi_suicide_plus2_by_* ndi_drug2_by_* ndi_mv_acc2_by_* ndi_other2_by_* ndi_firearm2_by_* ndi_assault2_by_* any_mis_notraffic_by_* any_domviolence_by_* arr_by_* any_oth_by_*  any_phys_by_* any_psychiatric_by_* any_phys_disab_by_* any_psych_disab_by_* any_phys_sep_by_* any_psych_sep_by_* any_othprof_by_* anydisability_in_* anyvadc_in_* anyssdissi_in_*  anyptsd_in_* anytbi_in_* ndi_death_by_* kia_by_* wia_by_* any_sigprofile_by_* sep_by_* volsep_by_* bar_or_misconduct_by_* disabsep_by_* any_profile_by_* nsc_enr_postarr_by_* nsc_asscplus_postarr_by_* nsc_bachplus_postarr_by_* demo_by_* bar_or_misconduct_by_* any_nvf_by_* any_vf_by_* any_oth_by_* any_mis_by_* any_drug_nvf_by_* inc_combined_by_* nsc_enr_pa_by_2017 nsc_enr_pa_by_2020 nsc_asscplus_pa_by_2017 nsc_asscplus_pa_by_2020 nsc_bachplus_pa_by_2017 nsc_bachplus_pa_by_2020 nsc_enroll_by_2017 nsc_enroll_by_2020 nsc_asscplus_by_2017 nsc_asscplus_by_2020 nsc_bachplus_by_2017 nsc_bachplus_by_2020 any_pgib_use any_pgib_use_by2017 any_pgib_use_by2020 any_p13_all2870731 any_premier_v1_2_all2870689 wia_serplus_by_* {
	count if `var' != 0 & `var' != 1 & `var' != .
	if r(N) == 0 replace `var' = `var' * 100
}
}

sort pid 
save "created_dta\analysis_samp_w_outcomes_final", replace 


